/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package facade;

import entity.Accion;
import entity.Inversor;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;

/**
 *
 * @author ari
 */
@Stateless
public class AccionFacade extends AbstractFacade<Accion> implements AccionFacadeLocal {
    @PersistenceContext(unitName = "BolsaValorWeb-ejbPU")
    private EntityManager em;

    @EJB
    private EstadisticaFacadeLocal estadisticaFacade;
    
    @EJB
    private EmpresaFacadeLocal empresaFacade;
    
    protected EntityManager getEntityManager() {
        return em;
    }

    public AccionFacade() {
        super(Accion.class);
    }

    @Override
    public void deleteAccion(Long id) {
        Accion a = em.createQuery("SELECT a FROM Accion a WHERE a.id = :id", Accion.class)
                .setParameter("id", id)
                .getSingleResult();
        
        remove(a);
    }

    @Override
    public List<String> getEmpresas() {
        return em.createQuery("SELECT DISTINCT a.name FROM Accion a", String.class)
                .getResultList();
    }

    @Override
    public Object[] consultaVentasEmp(String empresa) {
        return em.createQuery("SELECT SUM(a.num), MIN(a.monto), MAX(a.monto) FROM Accion a WHERE a.name = :empresa AND a.tipo = TRUE", Object[].class)
                .setParameter("empresa", empresa)
                .getSingleResult();
    }

    @Override
    public Object[] consultaComprasEmp(String empresa) {
        return em.createQuery("SELECT SUM(a.num), MIN(a.monto), MAX(a.monto) FROM Accion a WHERE a.name = :empresa AND a.tipo = FALSE", Object[].class)
                .setParameter("empresa", empresa)
                .getSingleResult();
    }

    @Override
    public Object[] obtenerInfo(Long id) {
        return em.createQuery("SELECT a.name, a.num, a.monto FROM Accion a WHERE a.id = :id", Object[].class)
                .setParameter("id", id)
                .getSingleResult();
    }

    @Override
    public List<Object[]> getComprasInv(String login) {
        return em.createQuery("SELECT a.id, a.name FROM Accion a JOIN a.inversor i WHERE i.login = :login AND a.tipo = FALSE ORDER BY a.name", Object[].class)
                .setParameter("login", login)
                .getResultList();
    }

    @Override
    public List<Object[]> getVentasInv(String login) {
        return em.createQuery("SELECT a.id, a.name FROM Accion a JOIN a.inversor i WHERE i.login = :login AND a.tipo = TRUE ORDER BY a.name", Object[].class)
                .setParameter("login", login)
                .getResultList();
    }

    @Override
    public void createVenta(String empresa, int num, double monto, String login) {
        int num1 = num;
        Inversor i = em.createQuery("SELECT i FROM Inversor i WHERE i.login = :login", Inversor.class)
                .setParameter("login", login)
                .getSingleResult();
                
        List<Accion> a = em.createQuery("SELECT a FROM Accion a JOIN a.inversor i WHERE a.name = :empresa AND i.login = :login AND a.monto = :monto AND a.tipo = TRUE", Accion.class)
                .setParameter("login", login)
                .setParameter("empresa", empresa)
                .setParameter("monto", monto)
                .getResultList();
        List<Accion> b = em.createQuery("SELECT a FROM Accion a WHERE a.name = :empresa AND a.monto >= :monto AND a.tipo = FALSE ORDER BY a.monto DESC", Accion.class)
                .setParameter("empresa", empresa)
                .setParameter("monto", monto)
                .getResultList();
        
        Accion accion = new Accion();
        if(b.isEmpty()) {
            estadisticaFacade.addEstadistica(0, num1, monto, 0, 0.0, empresa);
            empresaFacade.addEmpresa(empresa, 0.0, monto*num1);
            
            if(a.isEmpty()) {
                accion.setInversor(i);
                accion.setName(empresa);
                accion.setMonto(monto);
                accion.setNum(num);
                accion.setTipo(true);
                create(accion);        
            } else {
                accion = a.get(0);
                accion.setNum(accion.getNum()+num);
                edit(accion);
            }
            
        } else {
            if(a.isEmpty()) {
                accion.setInversor(i);
                accion.setName(empresa);
                accion.setMonto(monto);
                accion.setTipo(true);
            } else {
                accion = a.get(0);
                num += accion.getNum();
            }
            
            int aux = num;
            for(Accion temp : b) {
                if(num >= temp.getNum()) {
                    num -= temp.getNum();
                    remove(temp);
                    
                    if(num == 0) {
                        estadisticaFacade.addEstadistica(aux, num1, monto, 0, 0.0, empresa);
                        empresaFacade.addEmpresa(empresa, 0.0, monto*num1);
                        return;
                    }
                } else {
                    temp.setNum(temp.getNum() - num);
                    edit(temp);
     
                    estadisticaFacade.addEstadistica(num, num1, monto, 0, 0.0, empresa);
                    empresaFacade.addEmpresa(empresa, 0.0, monto*num1);
                    return;
                }
            }
            
            accion.setNum(num);
       
            estadisticaFacade.addEstadistica(aux-num, num1, monto, 0, 0.0, empresa);
            empresaFacade.addEmpresa(empresa, 0.0, monto*num1);
            
            create(accion);
        }

    }

    @Override
    public void createCompra(String empresa, int num, double monto, String login) {
        int num1 = num;
        
        Inversor i = em.createQuery("SELECT i FROM Inversor i WHERE i.login = :login", Inversor.class)
                .setParameter("login", login)
                .getSingleResult();
                
        List<Accion> a = em.createQuery("SELECT a FROM Accion a JOIN a.inversor i WHERE a.name = :empresa AND i.login = :login AND a.monto = :monto AND a.tipo = FALSE", Accion.class)
                .setParameter("login", login)
                .setParameter("empresa", empresa)
                .setParameter("monto", monto)
                .getResultList();
        List<Accion> b = em.createQuery("SELECT a FROM Accion a WHERE a.name = :empresa AND a.monto <= :monto AND a.tipo = TRUE ORDER BY a.monto", Accion.class)
                .setParameter("empresa", empresa)
                .setParameter("monto", monto)
                .getResultList();
        
        Accion accion = new Accion();
        if(b.isEmpty()) {
            /*
            if(!beanEstadistica.addEstadistica(0, 0, 0.0, num1, monto, empresa))
                return false;
            if(!beanEmpresa.addEmpresa(empresa, monto*num1, 0.0)) 
                return false;
            */
            if(a.isEmpty()) {
                accion.setInversor(i);
                accion.setName(empresa);
                accion.setMonto(monto);
                accion.setNum(num);
                accion.setTipo(false);
                create(accion);        
            } else {
                accion = a.get(0);
                accion.setNum(accion.getNum()+num);
                edit(accion);
            }
            
        } else {
            if(a.isEmpty()) {
                accion.setInversor(i);
                accion.setName(empresa);
                accion.setMonto(monto);
                accion.setTipo(false);
            } else {
                accion = a.get(0);
                num += accion.getNum();
            }
            
            int aux = num;
            for(Accion temp : b) {
                if(num >= temp.getNum()) {
                    num -= temp.getNum();
                    remove(temp);
                    /*
                    if(num == 0) {
                        if(!beanEstadistica.addEstadistica(aux, 0, 0.0, num1, monto, empresa))
                            return false;
                        if(!beanEmpresa.addEmpresa(empresa, monto*num1, 0.0)) 
                            return false;
                        return true;
                    }
                     * 
                     */
                        
                } else {
                    temp.setNum(temp.getNum() - num);
                    edit(temp);
                    /*
                    if(!beanEstadistica.addEstadistica(num, 0, 0.0, num1, monto, empresa))
                        return false;
                    if(!beanEmpresa.addEmpresa(empresa, monto*num1, 0.0)) 
                        return false;
                    
                    return true;
                    */
                }
            }
            
            accion.setNum(num);
            /*
            if(!beanEstadistica.addEstadistica(aux-num, 0, 0.0, num1, monto, empresa))
                return false;
            if(!beanEmpresa.addEmpresa(empresa, monto*num1, 0.0)) 
                return false;
            */
            create(accion);
            
        }

    }
    
    
}
